<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/3/22 0022
 * Time: 10:10
 */

namespace app\admin\controller;


use app\admin\model\AuthmenuModel;
use app\admin\model\AuthroleModel;
use think\Controller;
use think\Config;
use think\Loader;
use think\Db;

class AuthmenuController extends BaseController
{
    //主页
    public function index(){
        if (request()->isAjax()){
            $page=input("param.page");
            $limit=input("param.limit",100);
            $keyword=input("param.keyword");
            $where=[];
            $where="status in (1,2)";
            if (!empty($keyword)){
                $where="name like '%{$keyword}%'";
            }
            $sum=($page-1)*$limit;
            $data=Db::name("auth_menu")
                ->where($where)
                ->order("id ASC")
                ->limit("$sum,$limit")
                ->select();
            $num=Db::name("auth_menu")
                ->where($where)
                ->order("id ASC")
                ->select();
            $list=$this->rule($data);
            $result["code"]=0;
            $result["msg"]="";
            $result["count"]=count($num);
            $result["data"]=$list;
            return json($result);
        }
        return $this->fetch("index");
    }

    //处理权限和菜单列表
    public function rule($cate , $lefthtml = '— — ' , $pid=0 , $lvl=0, $leftpin=0 ){
        $arr=array();
        foreach ($cate as $v){
            if($v['parent_id']==$pid){
                $v['lvl']=$lvl + 1;
                $v['leftpin']=$leftpin + 0;//左边距
                $v['lefthtml']=str_repeat($lefthtml,$lvl);
                $arr[]=$v;
                $arr= array_merge($arr,self::rule($cate,$lefthtml,$v['id'],$lvl+1 , $leftpin+20));
            }
        }
        return $arr;
    }

//    菜单的添加
    public function add(){
        if (request()->isPost()){
            $list=request()->post();
            if ($list["parent_id"]==0){
                $list["type"]="menu";  //菜单栏控制
                $list["ico"]="fa-".input("ico");
            }elseif ($list["parent_id"]!=0){
                $find=Db::name("auth_menu")->where("id","=",$list["parent_id"])->find();
                if ($find["parent_id"]==0){
                    $list["type"]="menu";  //菜单栏控制
                }else{
                    $list["type"]="per";  //节点栏控制
                }
            }

            $findname=Db::name("auth_menu")->where("name","=",$list["name"])->find();
            if (!empty($findname)){
                return json(["code"=>0,"msg"=>"该菜单名称已存在"]);
            }
            $result=Db::name("auth_menu")->insert($list);
            if ($result){
                return json(["code"=>1,"msg"=>"添加成功"]);
            }else{
                return json(["code"=>0,"msg"=>"添加失败"]);
            }
        }
        $menu=Db::name("auth_menu")->where("type","=","menu")->select();
        $data=$this->rule($menu);
        $this->assign("data",$data);
        return $this->fetch("add");
    }

    /*
     * 编辑
     */
    public function edit(){
        $id=input("param.id");
        if (request()->isPost()){
            $list=request()->post();
            $result=Db::name("auth_menu")->where("id","=",$list["id"])->update($list);
            if ($result){
                return json(["code"=>1,"msg"=>"编辑成功"]);
            }else{
                return json(["code"=>0,"msg"=>"编辑失败"]);
            }
        }
        $where["id"]=["=",$id];
        $find=Db::name("auth_menu")->where($where)->find();
        $s_find=Db::name("auth_menu")->where("id","=",$find["parent_id"])->field("name")->find();
        $this->assign("find",$find);
        $this->assign("s_find",$s_find);
        return $this->fetch("edit");
    }

    /*
     * 删除
     */
    public function del(){
        $id=input("param.id");
        $model=new AuthmenuModel();
        $data=$model->del_menu($id);
        writelog("删除菜单成功");
        return json($data);
    }


}